﻿#If (Not NETFX_CORE) And (Not WINDOWS_PHONE) Then

Imports System.Net.Mail

Namespace Network

    Public Class Email

        Public Property SmtpUserName() As String = ""
        Public Property SmtpPassword() As String = ""
        Public Property SmtpAddress() As String = ""

        Public Property From() As MailAddress
        Public Property [To]() As MailAddress
        Public Property CC() As New MailAddressCollection()

        Public Property Subject() As String = ""
        Public Property Body() As String = ""
        Public Property Attachments() As New List(Of Attachment)

        Public Sub New()
            MyBase.New()
        End Sub

        Public Function Send(Optional ByVal pri As MailPriority = MailPriority.High) As Boolean
            Try
                If Me.From Is Nothing Then
                    Me.From = New MailAddress(Me.SmtpUserName, GetLeft(Me.SmtpUserName, "@"))
                End If
                Dim client As New SmtpClient(Me.SmtpAddress)
                With client
                    .UseDefaultCredentials = False
                    .Credentials = New System.Net.NetworkCredential(Me.SmtpUserName, Me.SmtpPassword)
                    .DeliveryMethod = SmtpDeliveryMethod.Network
                    If (Me.From Is Nothing Or Me.To Is Nothing) = False Then
                        Dim message As New MailMessage(Me.From, Me.To)
                        With message
                            .BodyEncoding = System.Text.Encoding.UTF8
                            .IsBodyHtml = False
                            .Body = Me.Body
                            .Subject = Me.Subject
                            .Priority = pri
                            For Each item In Me.Attachments
                                .Attachments.Add(item)
                            Next
                            For Each item In Me.CC
                                .CC.Add(item)
                            Next
                        End With
                        .Send(message)
                        Return True
                    End If
                End With
                Return False
            Catch ex As Exception
                Return False
            End Try
        End Function

    End Class

End Namespace

#End If